﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Sio.Mdm.Server.Administration
{
    /// <summary>
    /// Defines members for implementing server commands to be executed as part 
    /// of remote administration
    /// </summary>
    public interface IServerCommand
    {
        /// <summary>
        /// Get instance of server command is executed in
        /// </summary>
        IServer Server { get; set; }
        /// <summary>
        /// Parameters for command
        /// </summary>
        string[] Parameters { get; set; }
        /// <summary>
        /// Result of command interperted as string
        /// </summary>
        string Result { get; }
        /// <summary>
        /// Name of the command
        /// </summary>
        string Name { get; }
        /// <summary>
        /// Get description for command line help
        /// </summary>
        string Description{ get; }
        /// <summary>
        /// Should user be authorized or can command be executed without one
        /// </summary>
        bool AuthorizationRequired { get; }

        /// <summary>
        /// Execute command
        /// </summary>
        /// <returns>True if command complleted successfully, false otherwise</returns>
        bool Execute();
    }

    public class ServerCommand : IServerCommand
    {
        public ServerCommand(string name, params object[] args)
        {
            _name = name;
            
            if (args != null)
            {
                int count = args.Length;
                _params = new string[count];

                for (int i = 0; i < count; i++)
                {
                    _params[i] = args[i].ToString();
                }
            }            
        }
        public ServerCommand(string name)
            : this(name, null)
        {
        }

        #region IServerCommand Members
        private IServer _server;
        public IServer Server
        {
            get
            {
                return _server;
            }
            set
            {
                _server = value;
            }
        }
        public virtual string Description
        {
            get
            {
                return String.Empty;
            }
        }
        private string[] _params;
        public string[] Parameters
        {
            get 
            {
                return _params;
            }
            set
            {
                _params = value;
            }
        }

        private string _result;
        public string Result
        {
            get 
            {
                return _result;
            }
            protected set
            {
                _result = value;
            }
        }

        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
        }

        public virtual bool AuthorizationRequired
        {
            get
            {
                return true;
            }
        }

        public virtual bool Execute()
        {            
            return false;
        }

        #endregion
    }

}
